package com.ra.brand;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.googlecode.cswish.annotation.cm;
import com.ra.landlord.Building;
import com.ra.landlord.LandlordOwnerUser;
import com.ra.landlord.LandlordUser;

/**
 * 品牌公寓实体
 * 
 * @author colin
 * 
 */
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class BrandApartment {

	/**品牌公寓状态：申请中 */
	@cm("申请中")
	public static Integer STATUS_APPLYING = 0;
	
	/**品牌公寓状态：审核中 */
	@cm("待审核")
	public static Integer STATUS_AUDITING = 1;
	
	/**品牌公寓状态：已审核 */
	@cm("审核通过")
	public static Integer STATUS_AUDITED = 2;
	
	@cm("审核未通过")
	public static Integer STATUS_AUDITREJECTED = 3;
	
	/**品牌公寓banner：手机Banner */
	public static Integer BANNER_PHONE = 1;
	
	/**品牌公寓banner：电脑Banner */
	public static Integer BANNER_PC = 2;
	
	private Integer id;

	@cm("公寓名称")
	private String baName;
	
	@cm("公寓描述")
	private String baDesc;
	
	@cm("公寓最低价格")
	private BigDecimal lowPrice;

	/** (用于排序) */
	@cm("品牌公寓积分")
	private Integer score;

	@cm("品牌公寓图标 ")
	private File baIcon;
	private String baIconContentType;
	private String baIconFileName;

	/** 是否要分手机图片和电脑图片 */
	@cm("Banner图片")
	private List<BrandApartmentBanner> brandApartmentBanners;
	
	/** (手机Banner图片) */
	private List<BrandApartmentBanner> brandApartmentBannersPhone;
	
	/** (电脑Banner图片) */
	private List<BrandApartmentBanner> brandApartmentBannersPc;
	
	@cm("特色服务")
	private BACharactServiceItem baCharactServiceItem;

	@cm("推广房屋图片")
	private List<BrandPromotehouse> brandPromotehouses;
	
	@cm("品牌公寓房源代表图片")
	private String representRoomImg;
	
	@cm("公寓介绍图片")
	private List<BrandApartmentIntroducePic> brandApartmentIntroducePics;
	
	@cm("创建时间")
	private Date createTime;
	
	@cm("审核时间")
	private Date auditTime;

	@cm("所属房东")
	private LandlordOwnerUser landlordUser;

	@cm("创建人")
	private LandlordUser createUser;
	
	/** 0:申请中；1：审核中；2：审核通过 */
	@cm("状态")
	private Integer status;
	
	@cm("是否签约")
	private Boolean sign;
	
	@cm("发布的房源数")
	private Integer publishRoomNum;
	
	// @Transient
	private List<Building> buildings;
	
	private String logoImagePath;
	
	public String getLogoImagePath() {
		return logoImagePath;
	}

	public void setLogoImagePath(String logoImagePath) {
		this.logoImagePath = logoImagePath;
	}

	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Transient
	public List<Building> getBuildings() {
		return buildings;
	}

	public void setBuildings(List<Building> buildings) {
		this.buildings = buildings;
	}

	public String getBaName() {
		return baName;
	}

	public void setBaName(String baName) {
		this.baName = baName;
	}
	
	public String getBaDesc() {
		return baDesc;
	}

	public void setBaDesc(String baDesc) {
		this.baDesc = baDesc;
	}

	@Transient
	public File getBaIcon() {
		return baIcon;
	}

	public void setBaIcon(File baIcon) {
		this.baIcon = baIcon;
	}

	@Transient
	public String getBaIconContentType() {
		return baIconContentType;
	}

	public void setBaIconContentType(String baIconContentType) {
		this.baIconContentType = baIconContentType;
	}

	public String getBaIconFileName() {
		return baIconFileName;
	}

	public void setBaIconFileName(String baIconFileName) {
		this.baIconFileName = baIconFileName;
	}
	
	@OneToMany(mappedBy = "brandApartment", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = BrandApartmentBanner.class)
	public List<BrandApartmentBanner> getBrandApartmentBanners() {
		return brandApartmentBanners;
	}

	public void setBrandApartmentBanners(
			List<BrandApartmentBanner> brandApartmentBanners) {
		this.brandApartmentBanners = brandApartmentBanners;
	}

	@ManyToOne(targetEntity = BACharactServiceItem.class, cascade = {CascadeType.ALL}, fetch=FetchType.LAZY)
	public BACharactServiceItem getBaCharactServiceItem() {
		return baCharactServiceItem;
	}

	public void setBaCharactServiceItem(BACharactServiceItem baCharactServiceItem) {
		this.baCharactServiceItem = baCharactServiceItem;
	}

	@OneToMany(mappedBy = "brandApartment", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = BrandPromotehouse.class)
	public List<BrandPromotehouse> getBrandPromotehouses() {
		return brandPromotehouses;
	}

	public void setBrandPromotehouses(List<BrandPromotehouse> brandPromotehouses) {
		this.brandPromotehouses = brandPromotehouses;
	}
	
	@OneToMany(mappedBy = "brandApartment", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = BrandApartmentIntroducePic.class)
	public List<BrandApartmentIntroducePic> getBrandApartmentIntroducePics() {
		return brandApartmentIntroducePics;
	}

	public void setBrandApartmentIntroducePics(
			List<BrandApartmentIntroducePic> brandApartmentIntroducePics) {
		this.brandApartmentIntroducePics = brandApartmentIntroducePics;
	}

	public Integer getScore() {
		return score;
	}

	public void setScore(Integer score) {
		this.score = score;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public LandlordOwnerUser getLandlordUser() {
		return this.landlordUser;
	}

	public void setLandlordUser(LandlordOwnerUser landlordUser) {
		this.landlordUser = landlordUser;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public LandlordUser getCreateUser() {
		return createUser;
	}

	public void setCreateUser(LandlordUser createUser) {
		this.createUser = createUser;
	}
	
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	public Date getAuditTime() {
		return auditTime;
	}

	public void setAuditTime(Date auditTime) {
		this.auditTime = auditTime;
	}
	
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}
	
	public Boolean getSign() {
		return sign;
	}

	public void setSign(Boolean sign) {
		this.sign = sign;
	}
	
	public BigDecimal getLowPrice() {
		return lowPrice;
	}

	public void setLowPrice(BigDecimal lowPrice) {
		this.lowPrice = lowPrice;
	}
	
	public Integer getPublishRoomNum() {
		return publishRoomNum;
	}

	public void setPublishRoomNum(Integer publishRoomNum) {
		this.publishRoomNum = publishRoomNum;
	}
	
	@Transient
	public List<BrandApartmentBanner> getBrandApartmentBannersPhone() {
		List<BrandApartmentBanner> bannersPhone = getBannersByType(BrandApartment.BANNER_PHONE);
		if(bannersPhone == null) {
			return brandApartmentBannersPhone;
		} else {
			return bannersPhone;
		}
	}

	public void setBrandApartmentBannersPhone(
			List<BrandApartmentBanner> brandApartmentBannersPhone) {
		this.brandApartmentBannersPhone = brandApartmentBannersPhone;
	}

	@Transient
	public List<BrandApartmentBanner> getBrandApartmentBannersPc() {
		List<BrandApartmentBanner> bannersPc = getBannersByType(BrandApartment.BANNER_PC);
		if(bannersPc == null) {
			return brandApartmentBannersPc;
		} else {
			return bannersPc;
		}
	}

	public void setBrandApartmentBannersPc(
			List<BrandApartmentBanner> brandApartmentBannersPc) {
		this.brandApartmentBannersPc = brandApartmentBannersPc;
	}
	
	@Transient
	public String getRepresentRoomImg() {
		List<BrandPromotehouse> promotehouses = getBrandPromotehouses();
		if (CollectionUtils.isNotEmpty(promotehouses)) {
			representRoomImg = promotehouses.get(0).getHouseFileName();
		}
		return representRoomImg;
	}

	private List<BrandApartmentBanner> getBannersByType(Integer type){
		List<BrandApartmentBanner> newBrandApartmentBanners = null;
		if (this.brandApartmentBanners != null && type != null) {
			newBrandApartmentBanners = new ArrayList<>();
			for (BrandApartmentBanner brandApartmentBanner : this.brandApartmentBanners) {
				if (brandApartmentBanner.getType() == type) {
					newBrandApartmentBanners.add(brandApartmentBanner);
				}
			}
		} 
		return newBrandApartmentBanners;
	}

}